package com.xiangban.xiangbanmall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.xiangbanmall.dao.TicketMapper;
import com.xiangban.xiangbanmall.exception.BizException;
import com.xiangban.xiangbanmall.model.TUserTicket;
import com.xiangban.xiangbanmall.model.res.ApiResultCode;
import com.xiangban.xiangbanmall.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/12/27 10:17
 */
@Service
public class TicketService {

    @Autowired
    private TicketMapper ticketMapper;
    @Autowired
    private PageUtil pageUtil;

    public void convertTicket(TUserTicket ticket) {
        TUserTicket userTicketByTicketNo = ticketMapper.getUserTicketByTicketNo(ticket.getTicketNo());
        if (userTicketByTicketNo != null){
            ticketMapper.updateTicketStatusByTicketNo(ticket.getTicketNo());
        }else {
            throw new BizException(ApiResultCode.NOT_TICKET);
        }
    }

    public PageInfo<TUserTicket> listTicketPage(TUserTicket ticket) {

        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        List<TUserTicket> tUserTickets = ticketMapper.listTicket(ticket);
        return new PageInfo<>(tUserTickets);
    }
}
